package com.base.controller;

import com.base.comm.CommExcel;
import com.base.po.voip.*;
import com.base.service.*;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 旅游管理
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_TourGoods {
	@Autowired
	private TourGoodsService tourGoodsService;
	@Autowired
	private TourCategoryService tourCategoryService;
	@Autowired
	private UsersService usersService;

	// 自营商城分类首页
	@RequestMapping("/tourGoods")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		Page page = new Page();
		Users users = usersService.selectByPrimaryKey(adminUserId);
		
		//已有一级分类
		List<TourCategory> tourCategory = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			tourCategory = tourCategoryService.selectByLevel1(adminUserId,adminUserId);
		}
		
		//已有商品
		List<TourGoods> tourGoods = null;
		page.setCurrentPageNo(1);
		if(users.getRole() == 0 || users.getRole() == 1) {
			page.setRecordTotal(tourGoodsService.countByAll(adminUserId, adminUserId, null, null, null, null));
			tourGoods = tourGoodsService.selectByAll(page, adminUserId,adminUserId, null, null, null, null);
		}
		
		if(tourGoods != null && tourGoods.size() > 0) {
			for(TourGoods s : tourGoods) {
				if(s.getFirstclassify() != null) {
					try {
						s.setFirstclassify(tourCategoryService.selectByPrimaryKey(s.getFirstclassify()).getCategory());
					}catch (Exception e) {
						s.setFirstclassify("未查询到分类");
					}
				}else {
					s.setFirstclassify("未知分类");
				}
			}
			
			request.setAttribute("tourGoods", tourGoods);
		}
		if(tourCategory != null && tourCategory.size() > 0) {
			request.setAttribute("tourCategory", tourCategory);
		}
		request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		request.setAttribute("firstclassify", "分类");
		request.setAttribute("status", 3);
		CommExcel.couponList.clear();
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_TourGoods");
		return modelAndView;
	}
	
	@RequestMapping("/selectTourGoods")
	public ModelAndView selectTourGoods(HttpServletRequest request, Integer curPage, String goodstext,
			String firstclassify, String secondclassify, Byte status) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
	
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);
        
        //已有分类
        List<TourCategory> tourCategory = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			tourCategory = tourCategoryService.selectByLevel1(adminUserId,adminUserId);
		}
		
		//已有商品
		List<TourGoods> tourGoods = null;
		Integer choose = 2;
		if(goodstext != null && !goodstext.equals("") || !firstclassify.equals("分类") || status != 3) {
			choose = 1;
		}
		
		String fGoodsText = null;
		String fFirstClassify = null;
		String fSecondClassify = null;
		Byte fStatus = null;
		
		if(goodstext != null && !goodstext.equals("")) {
			fGoodsText = goodstext;
		}
		if(!firstclassify.equals("分类")) {
			fFirstClassify = firstclassify;
		}
		if(status != 3) {
			fStatus = status;
		}
		
		page.setCurrentPageNo(curPage);
		if(users.getRole() == 0 || users.getRole() == 1) {
			page.setRecordTotal(tourGoodsService.countByAll(adminUserId, adminUserId, fGoodsText, fFirstClassify, fSecondClassify,
					fStatus));
			tourGoods = tourGoodsService.selectByAll(page, adminUserId,adminUserId, fGoodsText, fFirstClassify, fSecondClassify,
					fStatus);
		}
		
		if(choose == 2) {
			if(users.getRole() == 0 || users.getRole() == 1) {
				page.setRecordTotal(tourGoodsService.countByAll(adminUserId, adminUserId, null, null, null, null));
				tourGoods = tourGoodsService.selectByAll(page, adminUserId,adminUserId, null, null, null, null);
			}
		}
		
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		if(tourGoods != null && tourGoods.size() > 0) {
			for(TourGoods s : tourGoods) {
				if(s.getFirstclassify() != null) {
					try {
						s.setFirstclassify(tourCategoryService.selectByPrimaryKey(s.getFirstclassify()).getCategory());
					}catch (Exception e) {
						s.setFirstclassify("未查询到分类");
					}
				}else {
					s.setFirstclassify("未知分类");
				}
			}
			
			request.setAttribute("tourGoods", tourGoods);
		}
		if(tourCategory != null && tourCategory.size() > 0) {
			request.setAttribute("tourCategory", tourCategory);
		}

		CommExcel.couponList.clear();
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_TourCategory");
		request.setAttribute("firstclassify", firstclassify);
		request.setAttribute("secondclassify", secondclassify);
		request.setAttribute("status", status);
		request.setAttribute("goodstext", goodstext);
		return modelAndView;
	}
}
